package com.xbp.examsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xbp.examsystem.common.Result;
import com.xbp.examsystem.entity.Banner;
import com.xbp.examsystem.service.BannerService;
import com.xbp.examsystem.service.FileUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/banners")
@Tag(name = "轮播图管理")
public class BannerController {
    @Autowired
    private BannerService bannerService;
    @Autowired
    private FileUploadService fileUploadService;

    @GetMapping("/list")
    @Operation(summary = "获取所有轮播图")
    public Result<List<Banner>> getBannerList(){
        List<Banner> list = bannerService.list();
        return Result.success(list,"获取所有轮播图成功！！");
    }

    @Operation(summary = "获取启用的轮播图")
    @GetMapping("/active")
    public Result<List<Banner>> getActiveBanner(){
        LambdaQueryWrapper<Banner> bannerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bannerLambdaQueryWrapper.eq(Banner::getIsActive,true);
        List<Banner> list = bannerService.list(bannerLambdaQueryWrapper);
        return Result.success(list,"获取启用的轮播图成功！！");
    }

    @Operation(summary = "根据ID查询轮播图")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        //调用BannerService中的getById方法
        Banner banner = bannerService.getById(id);
        return Result.success(banner,"根据id查询轮播图成功");
    }


    @Operation(summary = "添加轮播图")
    @PostMapping("/add")
    public Result add(@RequestBody Banner banner){
        boolean b = bannerService.save(banner);
        if(b){
            return Result.success("添加成功");
        }else{
            return Result.error("添加失败");
        }
    }

    @Operation(summary = "更新轮播图")
    @PutMapping("/update")
    public Result update(@RequestBody Banner banner){
        //调用BannerService中的updateById方法
        boolean b = bannerService.updateById(banner);
        if(b){
            return Result.success("更新成功");
        }else{
            return Result.error("更新失败");
        }
    }

    @Operation(summary = "根据id删除轮播图")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id){
        //调用BannerService中的removeById方法
        boolean b = bannerService.removeById(id);
        if(b){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

    @Operation(summary = "切换轮播图")
    @PutMapping("toggle/{id}")
    public Result toggleActiveStatus(@PathVariable Long id,@RequestParam Boolean isActive){
        LambdaUpdateWrapper<Banner> bannerLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        bannerLambdaUpdateWrapper.set(Banner::getIsActive,isActive).eq(Banner::getId,id);
        boolean b = bannerService.update(bannerLambdaUpdateWrapper);
        if(b){
            return Result.success("更新轮播图的状态成功");
        }else{
            return Result.error("更新轮播图的状态失败");
        }
    }

    @Operation(summary = "上传轮播图图片")
    @PostMapping("/upload-image")
    public Result uploadImage(MultipartFile file){
        String string = fileUploadService.uploadFile(file);
        return Result.success(string,"上传轮播图图片成功");
    }


}
